package com.tsunami.auth.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.ApplicationMapper;
import com.tsunami.auth.service.auth.IApplicationService;
import com.tsunami.auth.service.auth.IMenuService;
import com.tsunami.authority.dto.auth.ApplicationSaveDTO;
import com.tsunami.authority.dto.auth.ApplicationUpdateDTO;
import com.tsunami.authority.entity.auth.Application;
import com.tsunami.authority.entity.auth.Menu;
import com.tsunami.base.R;
import com.tsunami.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname ApplicationServiceImpl
 * @Description TODO
 * @Date 2022-03-09 17:04
 * @Created by lx
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements IApplicationService {
    @Autowired
    private IMenuService menuService;

    @Override
    public R selectPageList(Application data) {
        Page<Application> pages = baseMapper.selectPageList(PageUtils.buildPage(), data);
        return R.ok().data(pages);
    }

    @Override
    public void saveApp(ApplicationSaveDTO data, Long id) {
        Menu menu = new Menu();
        menu.setParentId(0l);
        menu.setDescribe(data.getDescribe());
        menu.setName(data.getName());
        menu.setSEnable(true);
        menu.setSPublic(true);
        menu.setCreateUser(id);
        menuService.save(menu);
        Application app = new Application();
//        app.setTenantId();
        app.setName(data.getName());
        app.setCode(data.getCode());
        app.setDescribe(data.getDescribe());
        app.setCreateUser(id);
        super.save(app);
    }

    @Override
    public void updateApp(ApplicationUpdateDTO data, Long id) {
        Application app = getById(data.getId());
        app.setName(data.getName());
        app.setDescribe(data.getDescribe());
        app.setUpdateUser(id);
        super.updateById(app);
    }

    @Override
    public boolean removeById(List<Long> ids) {
        if (ids.isEmpty()) {
            return true;
        }
        super.removeByIds(ids);
        return true;
    }

    @Override
    public boolean checkCode(String code) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("code", code);
        Application app = getOne(qw);
        if (app != null) {
            return true;
        }
        return false;
    }
}
